#!/usr/bin/python3

import asyncio
import websockets
import json
import sys
from keen import FileServer, Github

if len(sys.argv) == 2:
    FileServer.root_path = sys.argv[1]
else:
    FileServer.root_path = '/keen-file'
    
async def ws_main(websocket, path):
    github_code = await websocket.recv()
    res = Github.Login(github_code)
    if res.get('message') != None:
        await websocket.send('1')
        return
    elif res['login'] != 'guyaqi':
        await websocket.send('1')
        return
    await websocket.send('0')

    fs = FileServer()

    while True:
        cmd = await websocket.recv()
        await websocket.send(fs.exec(cmd))

start_server = websockets.serve(ws_main, '0.0.0.0', 30001)


asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()